<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\ExportWithTableName;
use App\Course;
use App\CourseTime;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Illuminate\Support\Facades\DB;

class CourseTimeController extends Controller
{
    use ModelForm;

    public function destroy($id)
    {
        if ($this->form()->destroy($id)) {
            return response()->json([
                'status' => true,
                'message' => trans('admin.delete_succeeded'),
            ]);
        } else {
            return response()->json([
                'status' => false,
                'message' => trans('admin.delete_failed'),
            ]);
        }
    }

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('所有课时');
            $content->description('列表');
            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('课时');
            $content->description('');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('课时');
            $content->description('新增');

            $content->body($this->form());
        });
    }

    public function create_with_course($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $course = Course::findOrFail($id);

            $content->header($course->name);
            $content->description('新增课时');

            $content->body(Admin::form(CourseTime::class, function (Form $form) use ($course) {
//                $month_ages = DB::select('SELECT id, title AS name  FROM month_ages');
//                $month_ages = array_combine(array_column($month_ages, 'id'), array_column($month_ages, 'name'));

                $form->display('id', 'ID');
                $form->hidden('course_id')->value($course->id);
                $time = date('Y-m-d H:i:s', time());
                $form->datetime('course_time', '上课时间')->rules("min:'$time'", [
                    'min' => '课程时间不能是过去时',
                ]);
                $form->number('max_sub', '人数')->default($course->max_sub);

                $form->display('created_at', 'Created At');
                $form->display('updated_at', 'Updated At');
            }));
        });
    }

    private function edit_course_time_form()
    {
        return Admin::form(CourseTime::class, function (Form $form) {
            $form->display('course.name', '课程');
            $time = date('Y-m-d H:i:s', time());
            $form->datetime('course_time', '上课时间')->rules("min:'$time'", [
                'min' => '课程时间不能是过去时',
            ]);
            $form->number('max_sub', '人数')->rules("min:'1'", ['min' => '人数要大于0']);
        });
    }

    public function edit_course_time($cid, $tid)
    {
        return Admin::content(function (Content $content) use ($cid, $tid) {

            $course = Course::findOrFail($cid);

            $content->header("<a href='/admin/course/{$course->id}/course_times'>{$course->name}</a>");
            $content->description('课时编辑');

            $content->body($this->edit_course_time_form()->edit($tid));
        });
    }

    public function update_course_time($cid, $tid)
    {
        return $this->edit_course_time_form()->update($tid);
    }

    public function destroy_course_time($cid, $tid)
    {
        if ($this->edit_course_time_form()->destroy($tid)) {
            return response()->json([
                'status' => true,
                'message' => trans('admin.delete_succeeded'),
            ]);
        } else {
            return response()->json([
                'status' => false,
                'message' => trans('admin.delete_failed'),
            ]);
        }
    }

    public function course_times($id)
    {

        return Admin::content(function (Content $content) use ($id) {
            $course = Course::findOrFail($id);
            $month_age = $course->month_age->title;
            $content->header('<a href="/admin/course">课程列表</a>');
            $content->description($course->name);

            $content->body(Admin::grid(CourseTime::class, function (Grid $grid) use ($id, $month_age) {
                $grid->filter(function ($filter) {
                    $filter->disableIdFilter();
                    $filter->between('course_time', '上课时间')->dateTime();
                });
                $grid->disableRowSelector();
                $grid->exporter(new ExportWithTableName('course_time_sub_count',
                    'course_times' . date('Y-m-d', time()),
                    ['id', 'course_name', 'month_age', 'course_time', 'max_sub', 'people_number'],
                    [],
                    ['ID', '课程名称', '月龄', '课程时间', '最大人数', '预约人数']
                ));

                $grid->model()->where('course_id', $id);
                $grid->id('ID')->sortable();
                //$grid->column('course.name', '课程名称');
                $grid->column('course.month_age_id', '月龄')->display(function () use ($month_age) {
                    return $month_age;
                });
                $grid->column('course_time', '课程时间')->sortable();
                $grid->column('max_sub', '最大人数')->sortable();
                $grid->column('people_number', '预约人数')->sortable();
            }));

        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $courses = DB::table('courses')->select()->get()->all();
        $courses = array_combine(array_column($courses, 'id'), array_column($courses, 'name'));
        $month_ages = DB::select('SELECT id, title AS name  FROM month_ages');
        $month_ages = array_combine(array_column($month_ages, 'id'), array_column($month_ages, 'name'));


        return Admin::grid(CourseTime::class, function (Grid $grid) use ($courses, $month_ages) {
            $grid->exporter(new ExportWithTableName('course_time_sub_count',
                'course_times' . date('Y-m-d', time()),
                ['id', 'course_name', 'month_age', 'course_time', 'max_sub', 'people_number'],
                [],
                ['ID', '课程名称', '月龄', '课程时间', '最大人数', '预约人数']
            ));

            $grid->filter(function ($filter) use ($courses, $month_ages) {
                $filter->disableIdFilter();
                $filter->equal('course_id', '课程')->select($courses);
                $filter->equal('course.month_age_id', '月龄')->select($month_ages);
                $filter->between('course_time', '上课时间')->dateTime();
//                $filter->equal('month_age_id', '月龄')->select($month_ages);
            });

            $grid->column('course.name', '课程名称');
            $grid->column('course.month_age_id', '月龄')->display(function ($month_age_id) use ($month_ages) {
                if (isset($month_ages[$month_age_id])) {
                    return $month_ages[$month_age_id];
                }
                return '—————';
            });
            $grid->column('course_time', '课程时间')->sortable();
            $grid->column('max_sub', '最大人数')->sortable();
            $grid->column('people_number', '预约人数')->sortable();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(CourseTime::class, function (Form $form) {

            $courses = DB::select('SELECT id, name FROM courses');
            $courses = array_combine(array_column($courses, 'id'), array_column($courses, 'name'));

//            $month_ages = DB::select('select id, title as name  from month_ages');
//            $month_ages = array_combine(array_column($month_ages, 'id'), array_column($month_ages, 'name'));

            $form->display('id', 'ID');
            $form->select('course_id', '课程')->options($courses);
            $time = date('Y-m-d H:i:s', time());
            $form->datetime('course_time', '上课时间')->rules("min:'$time'", [
                'min' => '课程时间不能是过去时',
            ]);
            $form->number('max_sub', '人数')->rules("min:'1'", ['min' => '人数要大于0']);
            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');
        });
    }
}
